<!--
  @name: 分类管理
  @author: lizibin
 -->
<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :data-callback="dataCallback" :isPageable="false">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" @click="openDrawer('新增')">新增</el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="success" link @click="openDrawer('编辑', scope.row)">编辑</el-button>
        <el-button type="primary" link @click="deleteAccount(scope.row)">删除</el-button>
      </template>
    </ProTable>
    <ClassifyDrawer ref="drawerRef" />
  </div>
</template>

<script setup lang="tsx" name="classifyManager">
import { ref, reactive } from "vue";
import { getClassifyInfo, deleteClassify, addClassify, editClassify } from "@/api/modules/allApi";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import ClassifyDrawer from "@/views/proTable/components/ClassifyDrawer.vue";
import { ProTableInstance, ColumnProps, HeaderRenderScope } from "@/components/ProTable/interface";

// ProTable 实例
const proTable = ref<ProTableInstance>();

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
  return getClassifyInfo();
};

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
  return {
    list: data,
    total: 0,
    pageNum: 1,
    pageSize: 10
  };
};

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { prop: "classifyName", label: "分类名称" },
  { prop: "classifyIntroduce", label: "分类介绍" },
  {
    prop: "articleNumber",
    label: "文章数量"
  },
  // {
  //   prop: "color1",
  //   label: "颜色1"
  // },
  // {
  //   prop: "color2",
  //   label: "颜色2"
  // },
  // {
  //   prop: "webmasterEmail",
  //   label: "合成渐变"
  // },
  { prop: "operation", label: "操作", fixed: "right", width: 200 }
]);

// 删除信息
const deleteAccount = async (params: any) => {
  await useHandleData(deleteClassify, { classifyId: params.classifyId }, `删除【${params.classifyName}】`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<any> | null>(null);
const openDrawer = (title: string, row: Partial<any> = {}) => {
  row.color1 = "#888888";
  row.color2 = "#888888";
  const params = {
    title,
    isView: false,
    row: { ...row },
    api: title === "新增" ? addClassify : editClassify,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
<style lang="scss">
.sty_link {
  color: rgb(47 167 185);
}
</style>
